import 'package:flutter/material.dart';

///
///
/// KeepAlive
///
/// 主要是针对PageView  SliverGrid或SliverList 中的控件进行缓存
/// https://api.flutter.dev/flutter/widgets/KeepAlive-class.html
///
///
///
class ScrollerCacheViewPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return PageViewAndTab();
  }
}



class PageViewAndTab extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return PageViewAndTabState();
  }
}


class PageViewAndTabState extends State<PageViewAndTab>  with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true; // 是否需要缓存

  final PageController _pageController = PageController();
  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("PageViewAndTab"),
      ),
      body: Center(
        child: PageView(
          controller: _pageController,
          children: [
      Container(
              color: Colors.blue,
              child: Center(
                child: Text("第一个页面"),
              ),
            ),
            Container(
              color: Colors.yellow,
              child: Center(
                child: Text("第二个页面"),
              ),
            ),Container(
              color: Colors.red,
              child: Center(
                child: Text("第三个页面"),
              ),
            ),
            Container(
              color: Colors.green,
              child: Center(
                child: Text("第4个页面"),
              ),
            ),
          ],
          onPageChanged: (index){
            setState(() {
              _selectedIndex = index;
            });
          },
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
            backgroundColor: Colors.red,
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'Business',
            backgroundColor: Colors.green,
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            label: 'School',
            backgroundColor: Colors.blue,
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.settings),
            backgroundColor: Colors.deepPurple,
            label: 'Settings',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: (int index){
          setState(() {
            _selectedIndex = index;
            _pageController.jumpToPage(_selectedIndex);
          });
        },
      ),
    );
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _pageController.dispose();
    super.dispose();
  }

}
